package cn.com.tarotframework.server.exception.controller;


import cn.com.tarotframework.exception.DemoException;
import cn.com.tarotframework.exception.TarotParam;
import cn.com.tarotframework.server.exception.po.Students;
import cn.com.tarotframework.server.exception.service.ExceptionService;
import cn.com.tarotframework.utils.response.TarotCode;
import cn.com.tarotframework.utils.response.TarotResponseResultBody;
import cn.com.tarotframework.utils.response.TarotResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.*;

@Validated
@Tag(name = "异常信息管理", description = "异常信息管理模块")
@RestController
@RequestMapping("exec")
@TarotResponseResultBody
public class ExceptionController {


    private final ExceptionService exceptionService;

    public ExceptionController(ExceptionService exceptionService) {
        this.exceptionService = exceptionService;
    }

    @Operation(summary = "验证业务异常0", method = "POST", description = "两个数字相除，除数为0")
    @PostMapping("div0")
    public int div0(
            @Parameter(name = "num1", description = "第一个数字", example = "10")
            @NotNull(message = "被除数不能为空")
            Integer num1,

            @Parameter(name = "num2", description = "第二个数字", example = "5")
//            @TarotParam(message = "不能为空", name = "num2")
            @NotNull(message = "除数不能为空")
//            @Min(message = "除数必须大于{value}", value = 1)
            Integer num2
    ) {

        return num1 / num2;
    }

    @Operation(summary = "验证业务异常1", method = "GET", description = "两个数字相除，除数为0")
    @GetMapping("div")
    public int div(
            @Parameter(name = "num1", description = "第一个数字", example = "10")
            @NotNull(message = "被除数不能为空")
            @RequestParam(name = "num1") Integer num1,

            @Parameter(name = "num2", description = "第二个数字", example = "5")
//            @TarotParam(message = "不能为空", name = "num2")
            @NotNull(message = "除数不能为空")
            @Min(message = "除数必须大于{value}", value = 1)
            @RequestParam(name = "num2", required = false) Integer num2
    ) {

        return num1 / num2;
    }

    @Operation(summary = "验证业务异常2", method = "GET", description = "抛异常：默认异常")
    @GetMapping("throw/extends")
    public void getExtends() {

        throw  new DemoException();

    }

    @Operation(summary = "验证业务异常3", method = "GET", description = "抛异常：自定义异常")
    @GetMapping("throw/auto")
    public void getAuto() {

        throw  new DemoException(3003, "自定义异常信息");

    }

    @Operation(summary = "验证业务异常4", method = "GET", description = "抛异常：自定义异常-msg")
    @GetMapping("throw/auto4")
    public void getAuto4() {

        throw  new DemoException("自定义异常信息-msg");

    }

    @Operation(summary = "验证业务异常5", method = "GET", description = "抛异常：自定义异常-Exception")
    @GetMapping("throw/ex")
    public void getEx() throws Exception {

//        throw  new DemoException(TarotCode.BUSINESS_EXCEPTION);

        throw  new Exception("asdfasdf");

    }

    @Operation(summary = "验证业务异常6", method = "GET", description = "抛异常：自定义异常-enum")
    @GetMapping("throw/enum")
    public void getEnum() {

//        throw  new DemoException(TarotCode.BUSINESS_EXCEPTION);

        throw  new DemoException(TarotCode.BUSINESS_EXCEPTION, "自定义异常-enum");

    }


    @Operation(summary = "验证业务异常7", method = "GET", description = "抛异常：自定义异常-Assert")
    @GetMapping("throw/assert")
    public void getAssert() {

        this.exceptionService.getException(1);

    }

    @Operation(summary = "验证业务异常8", method = "GET", description = "抛异常：自定义异常-Assert-Student")
    @GetMapping("throw/assert/{id}")
    public Students getAssert1(
            @NotNull(message = "学生ID不能为空")
            @Size(message = "学生ID长度在{min}~{max}之间", min = 5, max = 8)
            @PathVariable("id") String id
    ) {

        return this.exceptionService.getStudent(id);

    }

    @Operation(summary = "验证业务异常7", method = "GET", description = "抛异常：自定义异常-Assert-Student-query")
    @GetMapping("throw/assert/query")
    public Students getAssert2(
            @NotNull(message = "学生ID不能为空")
            @Size(message = "学生ID长度在{min}~{max}之间", min = 5, max = 8)
            @RequestParam("id") String id
    ) {

        return this.exceptionService.getStudent(id);

    }

    @Operation(summary = "自定义参数校验注解", method = "GET", description = "验证自定义参数校验注解TarotParam是否生效")
    @GetMapping("/auto/validation")
    public String getAutoValidate(
            @TarotParam(name = "name", message = "自定义注解--校验不能为空")
            @RequestParam("name") String name
    ) {

        return name;

    }

}
